package com.usian.service.impl;

import com.usian.MD5Utils;
import com.usian.mapper.TbUserMapper;
import com.usian.pojo.TbUser;
import com.usian.pojo.TbUserExample;
import com.usian.redis.RedisClient;
import com.usian.service.SsoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class SsoServiceImpl implements SsoService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Autowired
    private RedisClient redisClient;

    @Override
    public boolean checkUserInfo(String checkValue, int checkFlag) {

        //查询条件
        TbUserExample example =new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();

        //判断 checkFlag  1  username  2 phone
        if(checkFlag ==1){
            criteria.andUsernameEqualTo(checkValue);
        }else if(checkFlag ==2){
            criteria.andPhoneEqualTo(checkValue);
        }

        //查询  有结果  true  无结果  false
        List<TbUser> list = tbUserMapper.selectByExample(example);
        if(list.size()>0){
            return true;
        }

        return false;
    }

    @Override
    public boolean userRegister(TbUser tbUser) {
        //md5 密码加密
        String newPwd = MD5Utils.digest(tbUser.getPassword());
        tbUser.setPassword(newPwd);
        tbUser.setCreated(new Date());
        tbUser.setUpdated(new Date());
        int result  = tbUserMapper.insertSelective(tbUser);

        if(result>0){
            return true;
        }

        return false;
    }

    @Override
    public Map userLogin(String username,String password) {

        //验证用户名和密码
        String newPwd = MD5Utils.digest(password);
        TbUserExample example =new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(newPwd);
        List<TbUser> userList = tbUserMapper.selectByExample(example);

        if(userList.size()>0){

            //用户名和密码校验通过
            TbUser user = userList.get(0);
            //1:生成token  UUID
            String token = UUID.randomUUID().toString();    //uuxssw3ddsssws8888   前台  cookie   web有  app cookie  slit
            //2:用户信息存到redis   key token   value  用户    map

            redisClient.set("USER_INFO:"+token,user);
            redisClient.expire("USER_INFO:"+token,86400);

            Map<String,Object> map =new HashMap<>();

            map.put("token",token);
            map.put("userid",user.getId());
            map.put("username",user.getUsername());

           return map;
        }
        return null;
    }

    @Override
    public boolean getUserByToken(String token) {
        TbUser tbUser =(TbUser) redisClient.get("USER_INFO:"+token);
        if(tbUser!=null){
            //redisClient.set("USER_INFO:"+token,tbUser);
            redisClient.expire("USER_INFO:"+token,86400);
            return true;
        }
        return false;
    }
}
